package com.cmcm.aoplib;

import android.util.Log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.JoinPoint;

import java.io.IOException;
import java.net.URL;


@Aspect
public class DemoAspect {

    static final String TAG = "DemoAspect";
    @Pointcut("execution(* *..MainActivity.on*(..))")
    public void logForActivity(){};

    @Before("logForActivity()")
    public void log(JoinPoint joinPoint){
        Log.e(TAG, joinPoint.toShortString());
    }

    @Pointcut("execution(@SecurityCheckAnnotation public * *..*.*(..)) && @annotation(ann)")
    public void checkPermssion(SecurityCheckAnnotation ann){};

    @Before("checkPermssion(securityCheckAnnotation)")
    public void check(JoinPoint joinPoint,SecurityCheckAnnotation securityCheckAnnotation){
        String neededPermission = securityCheckAnnotation.declaredPermission();
        Log.e(TAG, "\tneeded permission is " + neededPermission);
        SecurityCheckManager manager = SecurityCheckManager.getInstanc();
        if(manager!=null&&manager.checkPermission(neededPermission) == false){
            throw new SecurityException("Need to declare permission:" + neededPermission);
        }
        return;
    }

    //TODO 这里可以放心大胆的aroud了，这样子就可以进行开关的判定了，也就是完成了aop 部分
    private static final String METHOD_CALL = "call(* *..MainActivity+.test(..)) && args(name)";
    @Pointcut(METHOD_CALL)
    public void methodCall(String name) {

    }
    @Around("methodCall(String)")
    public Object arouneMethodCall(ProceedingJoinPoint joinPoint){
        String name = (String) joinPoint.getArgs()[0];
        Log.e(TAG,name);
        return name;
    }


//     check url.getnnection

    public static final String URL_METHOD_CALL = "call (public java.net.URLConnection java.net.URL.openConnection(..))&& !within(com.cmcm.aoplib.*)";
    @Pointcut(URL_METHOD_CALL)
    public void urlconnection(){

    }

    @Around("urlconnection()")
    public Object checkPermissionAccpetted(ProceedingJoinPoint joinPoint) throws IOException, NetworkDeniedException {
        // check permission
//        Object args[] = joinPoint.getArgs();
//        for (Object o:args){
//            Log.e(TAG,o.toString());
//        }
        // first check switch
        NetPermissionCheck netPermissionCheck = NetPermissionCheckMgr.getIns().getNetPermissionCheck();
        if (netPermissionCheck!=null){
            boolean netPermissionAcceptted = netPermissionCheck.isNetPermissionAcceptted();
            if (!netPermissionAcceptted){
                //抛出一个异常，就可以保证不被不进行联网操作了
                throw new NetworkDeniedException();

            }
        }


        Object target = joinPoint.getTarget();
        Log.e(TAG,"checkPermissionAccpetted");
        if (target!=null){
            Class<?> aClass = target.getClass();
            Log.e(TAG,aClass.toString());
            Log.e(TAG,"not empty");
            Log.e(TAG, String.format("%d",Thread.currentThread().getId()));
            //
            if (target instanceof URL){
                return ((URL) target).openConnection();
//                return null;
            }

          }



        return null;



    }











}